package com.konne.nightmare.FastPublicOpinion.ui.information.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.adorkable.iosdialog.BottomSheetDialog;
import com.konne.nightmare.FastPublicOpinion.R;
import com.konne.nightmare.FastPublicOpinion.base.BaseMvpFragment;
import com.konne.nightmare.FastPublicOpinion.bean.HomeBean;
import com.konne.nightmare.FastPublicOpinion.bean.MessageEvent;
import com.konne.nightmare.FastPublicOpinion.http.BaseResponse;
import com.konne.nightmare.FastPublicOpinion.bean.OpinionDataBean;
import com.konne.nightmare.FastPublicOpinion.mvp.presenter.HomePresenter;
import com.konne.nightmare.FastPublicOpinion.mvp.view.IHomeFragmentView;
import com.konne.nightmare.FastPublicOpinion.ui.information.activity.ClassDetailActivity;
import com.konne.nightmare.FastPublicOpinion.ui.information.activity.GuiDangActivity;
import com.konne.nightmare.FastPublicOpinion.ui.information.activity.MainActivity;
import com.konne.nightmare.FastPublicOpinion.ui.information.activity.MembersManagerActivity;
import com.konne.nightmare.FastPublicOpinion.ui.information.adapter.Home1Adapter;
import com.konne.nightmare.FastPublicOpinion.ui.information.adapter.HomeAdapter;
import com.konne.nightmare.FastPublicOpinion.ui.information.adapter.HomeClassAdapter;
import com.konne.nightmare.FastPublicOpinion.ui.personal.activity.ReviseCodeActivity;
import com.konne.nightmare.FastPublicOpinion.utils.LoadProgressDialog;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


public class HomeFragment extends BaseMvpFragment<IHomeFragmentView, HomePresenter> implements IHomeFragmentView, View.OnClickListener {

    @BindView(R.id.home_layout)
    LinearLayout home_layout;
    @BindView(R.id.home_rec)
    RecyclerView home_rec;
    @BindView(R.id.home1_rec)
    RecyclerView home1_rec;
    @BindView(R.id.img_personal_center)
    ImageView imgPersonalCenter;
    @BindView(R.id.img_class_arrow)
    ImageView imgClassArrow;
    @BindView(R.id.rcl_class)
    RecyclerView rclClass;
    HomeAdapter homeAdapter;
    Home1Adapter home1Adapter;
    HomeClassAdapter homeClassAdapter;
    List<String> title = new ArrayList<>();
    List<String> title1 = new ArrayList<>();
    List<String> classList = new ArrayList<>();

    private OnPersonalClickListener onPersonalClickListener;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public int getLayout() {
        return R.layout.home_fragment;
    }

    @Override
    public void initView() {
        for (int i = 0; i < 4; i++) {
            title.add("i:" + i);
        }
        for (int i = 0; i < 4; i++) {
            title1.add("i:" + i);
        }
        for (int i = 0; i < 2; i++) {
            classList.add("i:" + i);
        }
        Log.d("eeeee", title.toString());
        homeAdapter = new HomeAdapter(R.layout.home_item, title);
        home_rec.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        home_rec.setAdapter(homeAdapter);
        homeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

            }
        });
        home1Adapter = new Home1Adapter(R.layout.home_item, title1);
        home1_rec.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        home1_rec.setAdapter(home1Adapter);
        home1Adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(new Intent(getActivity(), ClassDetailActivity.class));
            }
        });
        homeClassAdapter = new HomeClassAdapter(R.layout.item_have_a_class_home, classList);
        rclClass.setLayoutManager(new LinearLayoutManager(getActivity()));
        rclClass.setAdapter(homeClassAdapter);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        handleLoadData(false);
    }

    /***
     * 刷新与上拉
     */
    private void handleLoadData(boolean isScreen) {

    }

    /**
     * 提供Fragment实例
     *
     * @return
     */
    public static HomeFragment newInstance() {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRedPointStateChange(MessageEvent e) {
        if (null == e) {
            return;
        }

    }

    @Override
    public LoadProgressDialog getLoadDialog() {
        return dialog;
    }

    @Override
    public void cancelLoadDialog() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }


    @Override
    protected HomePresenter createPresenter() {
        return new HomePresenter();
    }


    @OnClick({R.id.gd_txt, R.id.img_personal_center,R.id.img_class_arrow})
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.login:
                Intent intent = new Intent();
                intent.setClass(getActivity(), GuiDangActivity.class);
                startActivity(intent);
                break;
            case R.id.img_personal_center:
                onPersonalClickListener.click();
                break;
            case R.id.img_class_arrow:
                if(rclClass.getVisibility()==View.VISIBLE){
                    imgClassArrow.setImageResource(R.mipmap.icon_blue_arrow_down);
                    rclClass.setVisibility(View.GONE);
                }else if(rclClass.getVisibility()==View.GONE){
                    imgClassArrow.setImageResource(R.mipmap.icon_blue_arrow_up);
                    rclClass.setVisibility(View.VISIBLE);
                }
                break;
        }
    }



    @Override
    public void myWarning(BaseResponse<List<HomeBean.RowsBean>> data, boolean isUpRefresh) {

    }

    @Override
    public void postOpinionList(BaseResponse<List<OpinionDataBean.ResponseDataBean>> data, boolean isUpRefresh) {


    }

    @Override
    public void postOpinionDataStatistics(BaseResponse<OpinionDataBean.ResponseStatisticalDataBean> data) {

    }

    @Override
    public void postOpinionCWBD(BaseResponse<String> data) {

    }

    @Override
    public void notMoreData() {

    }

    @Override
    public void responseFailed(String failed) {

    }

    public void setOnPersonalClickListener(OnPersonalClickListener listener) {
        this.onPersonalClickListener = listener;
    }

    public interface OnPersonalClickListener {
        void click();
    }
}
